app.controller('releOperatorCtrl', function($scope,$http,rest,$compile,$location,ngDialog,user,$state,$window,uiLoad,$rootScope,$ocLazyLoad,$timeout) {
	
	$scope.init = function(){
		
		//用于添加当前高亮
		$scope.moduleIndex = 0, 
		$scope.menuIndex = 0;
		
		//存储属树形结构
		$scope.myDatas = [];
		
		//请求select角色列表
    	rest.deco({
	        url: '/settlement-center-service/admin/role/list.do',  ///settlement-center-service/admin/role/list.do
	        data: {page:1,rows:100},
	        success: function(data){
	        	/**
	        	 返回数据string 用到了：
	        	 data.rows = Array 角色列表
	        	 data.rows[i].accesses = Array 用于匹配是否被选中
	        	 * **/
	          	var _data = JSON.parse(data);
	          	console.log(_data);
	          	$scope.$apply(function(){
	          	 	$scope.roles = _data.rows;
	          	 	$scope.selectedRole = $scope.roles[0];  //设置循环出来的selecte的默认选中项
	          	 	$scope.changeRole($scope.selectedRole);  //主动执行第一次
	          	})
	        }
	    })
    	
	}
	$scope.init();
	
	//改变授权角色的时候
	$scope.changeRole = function(obj){
		
		console.log(obj)
		
		//请求方法
		rest.deco({
	        url: '/settlement-center-service/admin/menuMethod/list.do',
	        data: {page:1,rows:100},
	        success: function(data){
	        	/**
	        	 返回数据string 用到了：
	        	 data.rows = Array 所有方法
	        	 * **/
	          	$scope.fn_datas = JSON.parse(data);
	        }
	    });
		
		//请求模块和菜单
		rest.deco({
	        url: '/settlement-center-service/admin/menu/list.do',
	        data: {page:1,rows:100},
	        success: function(data){
	        	/**
	        	 返回数据string 用到了：
	        	 data.rows = Array 所有的模块和菜单 
	        	 data.rows[i].level = 0 模块 
	        	 data.rows[i].level = 1 菜单 
	        	 * **/
	          	$scope._data = JSON.parse(data);
	          	console.log(	$scope._data)
	          	
	        	$scope.moduleDatas = []; //从数据中遍历出父级模块
	        	$scope.menuDatas = [];  //从数据中遍历出菜单模块
	        	
	        	angular.forEach($scope._data.rows,function(data,index,array){
	        		
	        		data.anniu = false; //默认给全部为false未选中
	        		data.itemId =  data.id;  //存储授权角色的ID
	        		data.roleId = obj.id;
	        		
	        		var _data = data;
	        		angular.forEach(obj.accesses,function(data,index,array){
	        			
	        			if(_data.id == data.menuId){ 
		        			_data.anniu = true;  //选中的为true
		        		}
	        			
	        			$scope.checkBox(_data); //切换按钮
	        			
	        		});
	        		
	        		if(data.level == 0){
	        			$scope.$apply(function(){
	        				$scope.moduleDatas.push(data);  //模块
	        			})
	        		} else{
	        			$scope.$apply(function(){
	        				$scope.menuDatas.push(data);  //菜单
	        			})
	        		};
	        		
	        	});
	        	
	        	//树形处理
	        	angular.forEach($scope.moduleDatas,function(data,index,array){
	        		var _data = data;
	        		var _arr = []; //匹配模块的子菜单
	        		angular.forEach($scope.menuDatas,function(data,index,array){
	        			if(_data.id == data.upId){
	        				_arr.push(data); //子菜单集合
	        			}
	        			var _fndata = data;
	        			var _fnArr = [];//匹配菜单的方法
	        			angular.forEach($scope.fn_datas.rows,function(data,index,array){
	        				
	        				data.anniu = false; //默认给全部为false未选中
	        				data.itemId =  _fndata.id+"_"+data.id;   //
	        				data.roleId = obj.id; //存储授权角色的ID
	        				
			        		var _data = data;
			        		angular.forEach(obj.accesses,function(data,index,array){
			        			if(_fndata.id == data.menuId && data.menuMethodId == _data.id){ 
				        			_data.anniu = true;  //选中的为true
				        		}
			        			$scope.checkBox(_data); //切换按钮
			        		});
			        		
			        		if(_fndata.menuName == data.upMenuName){
	        					_fnArr.push(data); //子方法集合
	        				}
	        				
	        			});
	        			_fndata.fangfa = _fnArr; //子方法添加到父级菜单
	        			
	        		});
	        		_data.caidan = _arr;  //子菜单添加到父级模块
	        	});
	        	console.log($scope.moduleDatas);  //完整的树形结构
	        	
	        	 
	        	$scope.$apply(function(){
	          	 	$scope.moduleDatas = $scope.moduleDatas;
	          	})
	        	
	        }
	    });
		
	}
	
	//切换按钮的方法
	$scope.checkBox = function(_data){
		
		_data.anniuFn = function(){  //选中按钮的方法 ng-click
			
			if(_data.anniu){ 
				layer.msg('确定移除权限吗？', {
		        	offset:'t',
		        	icon: 3,
					time: 0, //不自动关闭
					btn: ['确定', '取消'],
					yes: function(index){
						layer.close(index);
						
						//取消操作
						var arr = [];
						arr.push(_data.itemId);
						rest.deco({
					        url: '/settlement-center-service/admin/role/removeRoleAccess.do',
					        data: {"items[]":arr, "roleId":_data.roleId},
					        success: function(data){
					        	if(data.statusCode == "200"){
						          	layer.msg(data.message, {
								    	offset:'t',
								      	icon: 6,
								      	btn: ['确定']
								    });
					         		$scope.$apply(function(){
								    	_data.anniu = false;
								    });
							    } else{
							    	layer.msg(data.message, {
								    	offset:'t',
								      	icon: 5,
								      	btn: ['确定']
								    });
							    }
					        }
					    });
				    }
				});
			} else{
				layer.msg('确定添加权限吗？', {
		        	offset:'t',
		        	icon: 3,
					time: 0, //不自动关闭
					btn: ['确定', '取消'],
					yes: function(index){
						layer.close(index);
						
						//选中操作
						var arr = [];
						arr.push(_data.itemId);
						rest.deco({
					        url: '/settlement-center-service/admin/role/addRoleAccess.do',
					        data: {"items[]":arr, "roleId":_data.roleId},
					        success: function(data){
					        	if(data.statusCode == "200"){
						          	layer.msg(data.message, {
								    	offset:'t',
								      	icon: 6,
								      	btn: ['确定']
								    });
								    $scope.$apply(function(){
								    	_data.anniu = true;
								    });
							    } else{
							    	layer.msg(data.message, {
								    	offset:'t',
								      	icon: 5,
								      	btn: ['确定']
								    });
							    }
					        }
					   })
				    }
				});
			}
		}
		_data.anniuClass = function(){  //选中按钮的方法 ng-class
			var className = "";
			if(_data.anniu){
				className += "checkd";
			}
			return className;
		}
	}
	
	//当前高亮
	$scope.index = 0;
	$scope.moduleLtemactive = function(index){
		$scope.index = index;
	}
	$scope.moduleLtemClass = function(index){
		var className = "";
		if($scope.index == index){
			className += "active";
		}
		return className;
	}
	
})